/**
 * Created by zhangwx on 2015/10/15.
 */

var map;
var overlays = [];
var labels = [];
var drawingManager;
//初始化几个点图标备用
var Icons={};
var overlaycomplete = function (e) {
    overlays.push(e.overlay);
    labels.push(e.label);
};
$(function() {
    map = new BMap.Map('map_canvas', {enableMapClick: false});

    var mapStyle={  style : "grayscale" }
    map.setMapStyle(mapStyle);


    function myFun(result){
       // var cityName = result.name;
        map.centerAndZoom(result.center, result.level);
    }
    var localResult = new BMap.LocalCity(map);
    localResult.get(myFun);


    map.disableScrollWheelZoom();
  //  map.enableScrollWheelZoom(true);
    map.clearOverlays();
    map.disableDragging();     //禁止拖拽
    setTimeout(function () {
        map.enableDragging();   //两秒后开启拖拽
        map.enableInertialDragging();   //两秒后开启惯性拖拽
    }, 2000);


    var botton_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT, offset: new BMap.Size(10, 110)});// 左上角，添加比例尺
    map.addControl(botton_left_control);


    var styleOptions = {
        strokeColor: "#FF4500",    //边线颜色。
        fillColor: "#7B68EE",      //填充颜色。当参数为空时，圆形将没有填充效果。
        strokeWeight: 1,       //边线的宽度，以像素为单位。
        strokeOpacity: 0.8,	   //边线透明度，取值范围0 - 1。
        fillOpacity: 0.5,      //填充的透明度，取值范围0 - 1。
        strokeStyle: 'solid' //边线的样式，solid或dashed。
    }
//实例化鼠标绘制工具
     drawingManager = new BMapLib.DrawingManager(map, {
        isOpen: false, //是否开启绘制模式
        enableDrawingTool: true, //是否显示工具栏
        drawingToolOptions: {
            anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
            offset: new BMap.Size(-110, -10), //偏离值
            scale: 0.5
        },
        enableCalculate: true,
        circleOptions: styleOptions, //圆的样式
        polylineOptions: styleOptions, //线的样式
        polygonOptions: styleOptions, //多边形的样式
        rectangleOptions: styleOptions //矩形的样式
    });
//添加鼠标绘制工具监听事件，用于获取绘制结果

    drawingManager.addEventListener('overlaycomplete', overlaycomplete);


   /* var data;
    var selectCityList = new BMap.selectCityList(data);
    map.addControl(selectCityList);*/

    Icons={
        default:new BMap.Icon("http://api.map.baidu.com/img/markers.png",
            new BMap.Size(20,23), {
                anchor: new BMap.Size(10, 25),// 指定定位位置
                imageOffset: new BMap.Size(0, -274)// 设置图片偏移
            }
        ),
        red:new BMap.Icon("../static/images/map/marker_blue.png",
            new BMap.Size(16,20), {
                anchor: new BMap.Size(10, 25),
                imageOffset: new BMap.Size(-5,-3)// 设置图片偏移
            }
        ),
        /*green:new BMap.Icon("http://api.map.baidu.com/img/markers.png",
            new BMap.Size(20,23), {
                anchor: new BMap.Size(10, 25),
                imageOffset: new BMap.Size(0, 0 - 10 * 25)
            }
        ),*/
        orange:new BMap.Icon("http://api.map.baidu.com/img/markers.png",
            new BMap.Size(20,23), {
                anchor: new BMap.Size(10, 25),
                imageOffset: new BMap.Size(0, -300)
            }
        ),
        blue:new BMap.Icon("../static/images/map/marker_blue.png",
            new BMap.Size(16,20), {
                anchor: new BMap.Size(10, 25),
                imageOffset: new BMap.Size(-5,-3)
            }
        )

    };
    map.centerAndZoom("浙江省",9);




});

function clearAll() {
    for (var i = 0; i < overlays.length; i++) {
        map.removeOverlay(overlays[i]);
    }
    for (var i = 0; i < labels.length; i++) {
        map.removeOverlay(labels[i]);

    }
    overlays.length = 0;
    labels.length = 0;
}

